package com.tech.platform.cms.logger.aspect;

import com.tech.platform.cms.logger.annotation.ApiLog;
import com.tech.platform.cms.logger.publish.ApiLogPublish;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author shengqiang.wu
 * @date 2020/3/24/024 16:39
 * @desc
 */
@Aspect
@Component
public class ApiLogAspect {

    @Autowired
    private ApiLogPublish apiLogPublish;

    @Around("@annotation(apiLog)")
    public Object around(ProceedingJoinPoint point, ApiLog apiLog) throws Throwable{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        long beginTime = System.currentTimeMillis();
        Object object = point.proceed();
        long endTime =System.currentTimeMillis();
        long time = endTime-beginTime;
        apiLogPublish.publish(apiLog,request,point,time);
        return object;
    }
}
